<?php
class albumMod extends MY_Model{
	
	function albumMod(){
		parent::__construct(
			"album",
			array(
				"id_album",
				"id_member",
				"caover",
				"name",
				"tag",
				"description",
				"posted",
			),
			array("id_album")
		);
	}
	
	function _generateID($str){
    	return md5($str); 
    }
    
	/**
     * Insert data cuisine (menu makanan) baru. 
     * Contoh penggunaan:
     * </pre>
     * @param $newData (array) - array dari nama fields dan nilanya.
     */
    function Add($newData){
    	$newData["id_album"] = md5($newData.date("Y-m-d H:m:s"));
    	$newData["posted"] = date("Y-m-d H:m:s");
    	return $this->db->insert($this->table_name, $newData);
    }
    
    function update($id, $newData){
    	$this->db->where($this->table_fields[0], $id);
		return $this->db->update($this->table_name, $newData); 
    }
    
    /**
     * value dari tiap primary key field.
     * Contoh :
     * <pre>
     * 		//value dari tiap primary key field
     * 		$data = array(
     * 			'[id 1]',  		// primary key 1
     * 			'[id 2]',  		// primary key 2
     * 		);
     * </pre>
     * @param $leafID (array) - value dari tiap primary fields
     */
    function remove($ids){
    	
    	if (!is_array($ids)) $ids = array($ids);
    	foreach($ids as $pk) {
    		$this->db->or_where("id_album", $pk);	
    	}
    	
    	return $this->db->delete($this->table_name, $pk);
    }
    
    function removeImages($table_name,$id){
    	return $this->db->delete($table_name, $id);
    }
    
    function removeAlbum($table_images,$table_comment,$id){
//    	$this->load->model('imagesMod');
//    	$this->load->model('imagesComMod');    	
//    	$query = $this->imagesMod->get_album($id,0);
//    	//echo $query;
//    	foreach ($query as $row){
//    		$que = $this->imagesComMod->get($id);
//    		foreach ($que as $res){
//    			$this->db->delete('comment_images', array('id_images'=>$id));
//    		}
//			$this->db->delete('comment_album', array('id_images'=>$id));
//    		$this->imagesMod->remove($row->id_images);
//    	}
    	$this->db->delete($table_comment, array('id_album'=>$id));
    	$this->db->delete($table_images, array('id_album'=>$id));
    	$this->db->delete($this->table_name, array('id_album'=>$id));
    	
    	return true;
    }
    
    /**
     * return cuisine (menu makanan) dengan id tertentu. 
     * @param $ids (array) - id facility 
     */
    function get($id_member,$id){
		return $this->db->get_where($this->table_name, array("id_album"=>$id,'id_member'=>$id_member));
    }
	
	function get_records($id_member,$startOffset, $limit=20){
        return $this->db->get_where($this->table_name, array('id_member'=>$id_member), $limit, $startOffset);
//			return $this->db->query("select * from album where id_member='$id_member'");
    }
    
	function get_records_member($id_member,$startOffset, $limit=20){
        return $this->db->get_where($this->table_name, array('id_member'=>$id_member), $limit, $startOffset);
//			return $this->db->query("select * from album where id_member='$id_member'");
    }
}